package net.huashitong.service.money;

import net.huashitong.config.WeChatPayConfig;
import net.huashitong.service.user.SsUserLoginTokenService;
import net.huashitong.ssydt.money.entity.SsMoneyOrder;
import net.huashitong.ssydt.security.entity.SsUserLoginToken;
import net.huashitong.ssydt.security.utils.Sha1Util;
import net.huashitong.wxpay.RequestHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.08.16
 */
@Service
@Transactional
public class ChatPayServiceImpl implements ChatPayService {

    private Logger logger = LoggerFactory.getLogger(ChatPayServiceImpl.class);

    private WeChatPayConfig weChatPayConfig;

    @Autowired
    public ChatPayServiceImpl(WeChatPayConfig weChatPayConfig) {
        this.weChatPayConfig = weChatPayConfig;
    }

    @Autowired
    public SsUserLoginTokenService ssUserLoginTokenService;

    @Override
    public TreeMap<String, String> unifiedOrderForWeChat(SsMoneyOrder ssMoneyOrder, HttpServletRequest request, HttpServletResponse response) throws Exception {
        TreeMap<String, String> outParams = new TreeMap<String, String>();
        RequestHandler reqHandler = new RequestHandler(request, response);
        // 初始化
        reqHandler.init();
        WeChatPayConfig weChatPayConfig = new WeChatPayConfig();
        reqHandler.init(weChatPayConfig.getAppID(), weChatPayConfig.getKey(), weChatPayConfig.getMchID(), weChatPayConfig.getNotifyUrl());
        Integer money = (int) (ssMoneyOrder.getOrderPrice().doubleValue() * 100);//单位为分
//		Integer money = 1;//单位为分
        SsUserLoginToken ssUserLoginToken = ssUserLoginTokenService.getByUserId(ssMoneyOrder.getUserId());

        String entity = reqHandler.WeChatgenProductArgs(
                "华仕通教育-" + ssMoneyOrder.getOrderName(),
                ssMoneyOrder.getOrderId(),
                request.getRemoteAddr(),
                money.toString(),
                Sha1Util.getNonceStr(),
                ssUserLoginToken.getOpenId()
        );

        Map<String, String> map = reqHandler.unifiedorder(entity);
        if (map.get("return_code").equals("SUCCESS")) {
            // 签名参数列表
            outParams.put("appId", weChatPayConfig.getAppID());
            outParams.put("timeStamp", Sha1Util.getTimeStamp());
            outParams.put("nonceStr", Sha1Util.getNonceStr());
            outParams.put("package", "prepay_id=" + map.get("prepay_id"));
            outParams.put("signType", "MD5");
            String sign = reqHandler.genPackageSign(outParams);
            outParams.put("paySign", sign);
        } else {
            outParams.put("error", "发起微信支付失败，请联系客服");
        }
        return outParams;
    }


}
